<template>
	<view class="">
		<form @submit="formSubmit">
			<view class="list">
				<view class="acea-row row-middle row-between item border-b ">
					<view class="label label-m">
						区域
					</view>
					<!-- <view class="acea-row row-middle row-right item-r sanjiao-right">
						<view class="no-data-r">请选择区域</view>
					</view> -->

					<view class="item-r sanjiao-right" @click="goServiceArea">
						<input class="input-r" type="text" placeholder="请选择区域" placeholder-class="placeholder-class"
							v-model="regionName" disabled />
					</view>
				</view>
				<view class="acea-row row-middle row-between item border-b ">
					<view class="label label-m">
						地址
					</view>
					<view class="acea-row row-middle row-right item-r address">
						<input type="text" placeholder="请输入/选择地址" v-model="addres" name='addres' @input="fillTitle">
						<image src="../static/images/location.png" mode="" @click="clickMap"></image>
					</view>
				</view>
				<view class="acea-row row-middle row-between item border-b " v-if="typeOn == 1">
					<view class="label">转让费</view>
					<view class="acea-row row-middle row-right item-r">
						<view class="acea-row row-middle row-right small-ipt">
							<input type="number" placeholder="示例:30" placeholder-class="placeholder-class"
								name="transferFee" v-model="dataform.transferFee">
							<text>万元</text>
						</view>
						<view class="acea-row row-middle meet" :class="transferStatus==1?'':'no-meet'">
							<view class="meet-text">面议</view>
							<view class="switch" @click="transferStatus = transferStatus == 0? 1 : 0">
								<image src="../static/images/switch_on.png" mode="" v-if="transferStatus == 1"></image>
								<image src="../static/images/switch_off.png" mode="" v-else></image>
							</view>
						</view>
					</view>
				</view>
				<view class="acea-row row-middle row-between">
					<view class="acea-row row-middle row-between item border-b w-48">
						<view class="label label-m">产权面积</view>
						<view class="acea-row row-middle row-right small-ipt">
							<input type="number" placeholder="示例:30" placeholder-class="placeholder-class"
								v-model="bvileAvea" name='bvileAvea'>
							<text>m²</text>
						</view>
					</view>
					<view class="acea-row row-middle row-between item border-b w-48">
						<view class="label label-m">使用面积</view>
						<view class="acea-row row-middle row-right small-ipt">
							<input type="number" placeholder="示例:30" placeholder-class="placeholder-class"
								v-model="avea" name='avea' @input="fillTitle">
							<text>m²</text>
						</view>
					</view>
				</view>
				<view class="acea-row row-middle row-between item border-b ">
					<view class="label">月租金</view>
					<view class="acea-row row-middle row-right item-r">
						<view class="acea-row row-middle row-right small-ipt">
							<input type="number" placeholder="示例:3000" placeholder-class="placeholder-class"
								v-model="rent" name='rent'>
							<text>元/月</text>
						</view>
						<view class="acea-row row-middle meet" :class="isFace== 1?'':'no-meet'">
							<view class="meet-text">面议</view>
							<view class="switch" @click="isFace = isFace== 0?1:0">
								<image src="../static/images/switch_on.png" mode="" v-if="isFace == 1"></image>
								<image src="../static/images/switch_off.png" mode="" v-else></image>
							</view>
						</view>
					</view>
				</view>
				<view class="acea-row row-middle row-between item border-b">
					<view class="label">付款方式</view>
					<view class="acea-row row-middle row-right item-r pay-type">
						<view class="">付</view>
						<input class="pay-month" type="number" v-model="month" name='month'>
						<view class="">月</view>
						<view class="margin-l">押金</view>
						<view class="margin-l">押</view>
						<input class="pay-money" type="number" v-model="deposit" name='deposit'>
						<!-- <view class="">元</view> -->
					</view>
				</view>
				<!-- 优势标签 -->
				<view class="module border-b">
					<!-- <view class="acea-row row-middle row-between">
						<view class="module-title">
							优势标签
						</view>
						<view class="add-advantage red-sanjiao-r" @click="addAd">
							没找到合适的？选择自定义
						</view>
					</view>
					<view class="acea-row advantage-list">
						<view class="itemBox" v-for="(item,index) in advantages" :key="index">
							<icon type="cancel" color="#000" size="14" @click="closeAdvantages(item,index)"
								class="close" />
							<view class="item" :class="advantageIds.indexOf(index)!=-1?'item-on':''"
								@click="advantageChecked(item,index)">{{item}}</view>
						</view>
					</view> -->
					<chooseKeywords title="优势标签" :advantages="advantages" type="0" :typeOn="typeOn"
						:storeAdvantage="storeAdvantage" :advantageIds="advantageIds"
						@storeAdvantage="val=>(storeAdvantage=val)" @advantageIds="val=>(advantageIds=val)"
						@advantages="val=>(advantages=val)" />
				</view>
				<view class="acea-row row-middle row-between item border-b ">
					<view class="label">
						标题
					</view>
					<view class="acea-row row-middle row-right item-r">
						<input class="input-r" type="text" placeholder="请输入标题" placeholder-class="placeholder-class"
							v-model="title" name='title' />
					</view>
				</view>
				<!-- 联系人 -->
				<view class="acea-row row-middle row-between item ">
					<view class="label label-m">
						联系人
					</view>
					<view class="add-icon" @click="addContact">
						<image src="../static/images/add_icon.png" mode=""></image>
					</view>
				</view>
				<view class="item border-b">
					<!-- <view class="contact-list">
						<view class="acea-row row-middle row-between contact-p" v-for="(item,index) in contactList"
							:key="index">
							<view class="acea-row row-middle row-between contact-r">
								<view class="add-icon" @click="deleteContact(index)">
									<image src="../static/images/sub_icon.png" mode=""></image>
								</view>
								<view class="name" @click="toPage('/pages/releases/addShopContact/addShopContact?info='+JSON.stringify(item) + '&infoIndex='+index)">{{item.username}}</view>
								<view class="gray-text" v-if='item.type == 1'>
									房东
								</view>
								<view class="gray-text" v-if='item.type == 2'>
								老板
								</view>
								<view class="gray-text" v-if='item.type == 3'>
									雇员
								</view>
								<view class="gray-text"
									@click="toPage('/pages/releases/addShopContact/addShopContact?info='+JSON.stringify(item) + '&infoIndex='+index)">
									{{item.phone}}
								</view>
							</view>
							<view class="acea-row row-middle check-box" v-if="item.open == 0">
								<image src="../static/images/checkbox_no.png" mode="" @click="openChange(item,1)">
								</image>
								<text>公开</text>
							</view>
							<view class="acea-row row-middle check-box" v-if="item.open == 1">
								<image src="../static/images/checkbox_on.png" mode="" @click="openChange(item,0)">
								</image>
								<text>公开</text>
							</view>
						</view>
					</view> -->
					<chooseContact :list="contactList" :dataform="dataform" @add="d=>contactList.push(d)"
						@del="toDel" />

				</view>
				<!-- 描述 -->
				<view class="module">
					<view class="module-title">
						描述
					</view>
					<view class="textacea-box">
						<textarea placeholder="请输入描述" v-model="details" name='details'
							:maxlength="maxLength"></textarea>
						<view class="num-text">
							{{details.length}}/{{maxLength}}
						</view>
					</view>
				</view>
				<!-- 上传照片/视频 -->
				<view class="module">
					<view class="acea-row row-middle row-between">
						<view class="module-title">
							上传照片/视频
						</view>
						<view class="example-text" @click="getImage">
							图片示例
						</view>
					</view>
					<view class="example-text-l">
						请至少添加一张正面照，图片越多审核越快
					</view>
					<view class="acea-row pictrue-list">
						<view class="acea-row row-column row-center-wrapper pictrue" v-for="item in PhotoList"
							:key="item.url">
							<image style="width: 218rpx;height: 172rpx;" :src="item.url" mode=""
								v-if="item.type=='image'"></image>
							<video style="width: 218rpx;height: 172rpx;" :src="item.url" mode="" v-else></video>
						</view>
						<view class="acea-row row-column row-center-wrapper pictrue" @click="uploadImg"
							v-if="PhotoList.length < 10">
							<view class="upload-icon">
								<image src="../static/images/upload_icon.png" mode=""></image>
							</view>
							<view>添加照片/视频</view>
						</view>
					</view>
				</view>
			</view>

			<button class="submit-btn" form-type="submit">
				完成
			</button>
		</form>
		<serviceArea ref="serviceArea" dataType="area" @area="handleArea" />
		<ImageInstance ref="ImageInstance"></ImageInstance>
		<addKeyWord ref="addKeyWord" @addConfirm="addConfirm" />
	</view>
</template>

<script>
	import {
		Debounce
	} from '@/utils/validate.js'
	import serviceArea from '../components/area.vue';
	import ImageInstance from '../components/ImageInstance.vue'
	import addKeyWord from '../components/addKeyWord/addKeyWord.vue'
	import chooseKeywords from '../components/chooseKeywords.vue'
	import {
		toPage
	} from '@/filters/commFilter.js'
	import {
		informationAdd,
		ShopRentDetailApiNoDw,
		editInfo
	} from '@/api/subletOfShop.js'
	import chooseContact from '../components/chooseContact.vue'
	export default {
		props: ['categoryOn', 'typeOn', 'editId', 'patientinfo', 'st', 'storeCategory'],
		components: {
			serviceArea,
			ImageInstance,
			addKeyWord,
			chooseKeywords,
			chooseContact
		},
		data() {
			return {
				addres: this.$address, //地址
				bvileAvea: '', //产权面积
				avea: '', //使用面积
				rent: '', //月租金
				isFace: 0, //月租金是否面议
				month: 6, //付款方式月
				deposit: 10000, //付款方式押金
				title: '', //标题
				contactList: [],
				advantages: [], //店铺优势
				advantageIds: [],
				maxLength: 200,
				details: '', //补充
				storeAdvantage: [],
				PhotoList: [],
				hopeArea: '', //期望区域id
				regionName: '', //期望区域名称
				location: {},
				dataform: {
					status: '',
					businessCircle: '',
					businessName: ''
				},
			}
		},
		watch: {
			avea() {
				this.fillTitle();
			},
			patientinfo: {
				handler: function(newV, oldV) {
					console.log("newV", newV)
					if (newV && Object.keys(newV).length > 0) {
						this.dataform = {
							...this.dataform,
							...newV
						}
						this.dataform.bvileAvea = this.dataform.floorage
						this.bvileAvea = this.dataform.floorage
						let d = this.dataform;
						getApp().globalData.fbDeal(d, this)
						this.supportingFacility = this.dataform.supportingName;
						this.storefrontTypeName = this.dataform.storefrontName
						this.floor = this.dataform.floor[0].split('-')
						this.avea = this.dataform.avea
						console.log(this.dataform)
					}
				},
				deep: true,
			}
		},
		mounted() {
			let that = this
			that.location = that.$location;
			this.getBusiness(that.location);
			getApp().globalData.initContact()
			this.dataform.city = this.$cityCode ? this.$cityCode[1] : '';
			this.dataform.province = this.$cityCode ? this.$cityCode[0] : '';
			this.dataform.district = this.$cityCode ? this.$cityCode[2] : '';
			if (this.editId) {
				this.getDetail();
			}
		},
		methods: {
			toPage,
			//添加配套要求
			addAd() {
				this.$refs.addKeyWord.popupShow = true
			},
			toDel(index, info, callback) {
				if (info) {
					this.contactList.splice(index, 1, info);
				} else {
					this.contactList.splice(index, 1);
				}
				callback && callback();
			},
			addConfirm(e) {
				this.advantages = this.advantages.concat(e)
			},

			//查看图片示例
			getImage() {
				this.$refs.ImageInstance.show = true
			},
			//去选择期望区域
			goServiceArea() {
				this.$refs.serviceArea.filtrateShow = true;
			},
			//期望区域
			handleArea(areaInfo) {
				this.hopeArea = areaInfo.districtIds;
				this.regionName = areaInfo.regionName;
			},
			//自动填充标题 地址+ 面积+出租
			fillTitle() {
				if (this.editId && this.title) {
					return;
				}
				this.title = this.avea ? ((this.typeOn == 0 ? '出租' : '转让') + (this.dataform.businessName ? this.dataform
					.businessName : '') + this.avea + '㎡土地') : this.title
			},
			formSubmit: Debounce(function(e) {
				let that = this
				// console.log(that.typeOn,e.detail.value['status'] = 1)
				if (!that.hopeArea) return that.$util.Tips({
					title: '请选择区域'
				});
				if (!that.addres) return that.$util.Tips({
					title: '请输入/选择地址'
				});
				if (!that.bvileAvea) return that.$util.Tips({
					title: '请输入产权面积'
				});
				if (!that.avea) return that.$util.Tips({
					title: '请输入使用面积'
				});
				if (!that.contactList || that.contactList.length == 0) return that.$util.Tips({
					title: '请输入联系人'
				});
				if (that.categoryOn == 5) {
					e.detail.value.storeCategory = 912
				}

				if (that.typeOn == 0) {
					e.detail.value['status'] = '0'
					// e.detail.value.status = '0'
				}
				e.detail.value.isFace = that.isFace
				e.detail.value.expectedRegion = that.hopeArea
				e.detail.value.storeAdvantage = that.storeAdvantage.join('/')
				e.detail.value.contacts = that.contactList.map(item => {
					const {
						id,
						type,
						open,
						were
					} = item;
					return {
						contactId: id,
						identity: type,
						open,
						were
					}
				})
				//支付方式
				e.detail.value.modePayment = '付' + this.month + '月,押' + this.deposit + '元'

				e.detail.value.shopfrontPhoto = that.PhotoList ? that.PhotoList.map(item => item.url).join(",") :
					''
				e.detail.value.storeCategory = 912
				if (that.typeOn == 0) {
					this.dataform['status'] = '0'
				} else if (that.typeOn == 1) { //转让
					this.dataform['status'] = '2'
				}
				e.detail.value = {
					...this.dataform,
					...e.detail.value,
					...that.location
				};

				if (this.editId) {
					e.detail.value.id = this.editId;
					editInfo(e.detail.value).then(res => {
						if (res.code == 200) {
							this.$util.Tips({
								title: '编辑成功'
							}, {
								tab: 3
							})
							// uni.navigateBack()
						} else {
							this.$util.Tips({
								title: '编辑失败'
							})
						}

					}).catch(e => {
						this.$util.Tips({
							title: e
						})
					})
				} else {
					informationAdd(e.detail.value).then(res => {
						if (res.code == 200) {
							that.$util.Tips({
								title: '发布成功'
							}, {
								tab: 2,
								url: '/pages/releases/rentalStores/rentalStores'
							})
						}
					}).catch(e => {
						this.$util.Tips({
							title: e
						})
					})
				}


			}),
			clickMap(success) {
				uni.chooseLocation({
					success: (res) => {
						this.addres = res.address
						this.$set(this.location, 'latitude', res.latitude)
						this.$set(this.location, 'longitude', res.longitude)
						this.getBusiness({
							longitude: res.longitude,
							latitude: res.latitude,
						})
					},
				})
			},
			getBusiness(location) {
				let that = this;
				getApp().globalData.getBusinessList(location).then(res => {
					if (res.code == 200) {
						console.log("定位获取到的商圈", res)
						that.dataform.province = res.data.province;
						that.dataform.city = res.data.city;
						that.dataform.district = res.data.district;
						that.dataform.businessName = res.data.businessName;
						that.dataform.businessCircle = res.data.businessId;
						console.log(that.dataform.businessName)
						that.fillTitle()
					}
				});
			},
			//上传照片
			uploadImg() {
				uni.showActionSheet({
					title: '选择上传类型',
					itemList: ['图片', '视频'],
					success: res => {
						if (res.tapIndex == 0) {
							this.chooseImages();
						} else {
							this.chooseVideo();
						}
					}
				});
			},
			//选择照片
			chooseImages() {
				let that = this;
				that.$util.uploadImageOne({
					url: 'upload/image',
					count: 5,
					name: 'multipart',
					model: "user",
					pid: 0
				}, function(res) {
					console.log(res)
					that.PhotoList.push({
						url: res.data.url,
						type: 'image'
					})
				});
			},
			//选择视频
			chooseVideo() {
				let that = this;
				that.$util.uploadVideo({
					url: 'upload/image',
					name: 'multipart',
					model: "user",
					pid: 0
				}, function(res) {
					console.log(res)
					that.PhotoList.push({
						url: res.data.url,
						type: 'video'
					})
				});

			},
			openChange(row, index) {
				row.open = index
			},
			deleteContact(index) {
				this.contactList.splice(index, 1)
			},
			//删除标签
			closeAdvantages(item, index) {
				this.advantages.splice(index, 1)
				if (this.advantageIds.indexOf(index) != -1) {
					let idx = this.advantageIds.indexOf(index)
					this.advantageIds.splice(idx, 1);
					this.storeAdvantage.splice(index, 1)
				}
			},
			advantageChecked(item, index) {
				if (this.advantageIds.indexOf(index) != -1) {
					let idx = this.advantageIds.indexOf(index)
					this.advantageIds.splice(idx, 1);
					this.storeAdvantage.splice(item, 1)
				} else {
					this.advantageIds.push(index);
					this.storeAdvantage.push(item)
				}
			},
			addContact() {
				uni.navigateTo({
					url: '/pages/releases/addShopContact/addShopContact'
				})
			},
			getDetail() {
				ShopRentDetailApiNoDw(this.editId, {
					status: this.st,
					storeCategory: this.storeCategory
				}).then(res => {
					console.log('editShop', res)
					if (res.code == 200) {
						this.dataform = res.data
						// this.supportingFacility = this.dataform.supportingFacility
						// this.groupCurrentName = this.dataform.groupCurrent
						// this.workingGroupName = this.dataform.workingGroup
						let d = res.data
						getApp().globalData.fbDeal(d, this)
						this.PhotoList = this.shopfrontPhotoList ? this.shopfrontPhotoList : [],
							console.log('this.PhotoList', this.PhotoList)
						this.PhotoList = this.PhotoList.map(item => ({
							url: item,
							type: (item.indexOf('.mp4') == -1 ? 'image' : 'video')
						}))
						console.log('this.PhotoList', this.PhotoList)
						// let notFiled = ['contacts', 'storeAdvantage', 'area']
						// for (let k in this.dataform) {
						// 	if (this.hasOwnProperty(k)) {
						// 		if (notFiled.indexOf(k) == -1) {
						// 			this[k] = this.dataform[k]
						// 		}
						// 	}
						// }
						// if (d.area) {
						// 	this.area1 = d.area.split('-')[0]
						// 	this.area2 = d.area.split('-')[1]
						// }

						// if (this.dataform.contacts && this.dataform.contacts.length > 0) {
						// 	this.contactList = this.dataform.contacts.map(item => ({
						// 		...item,
						// 		username: item.contactName,
						// 		phone: item.contactPhone,
						// 		id: item.contactId,
						// 		infoId: item.id,
						// 		type: item.contactType
						// 	}))
						// 	delete this.dataform.contacts
						// }
						// this.hopeArea = d.expectedRegion;
						// this.regionName = d.expectedRegionName;
						// this.dataform.storeAdvantage && this.dataform.storeAdvantage.split('/').forEach((item,
						// 	index) => {
						// 	let ind = this.advantages.indexOf(item);
						// 	if (ind == -1) {
						// 		this.advantages.push(item)
						// 		this.storeAdvantage.push(item)
						// 		this.advantageIds.push(this.advantages.length - 1)
						// 	} else {
						// 		this.advantageIds.push(ind)
						// 		console.log(this.storeAdvantage)
						// 		this.storeAdvantage.push(this.advantages[ind])
						// 	}

						// })
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '/static/css/releaseForm.css';
</style>